#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "loginCheck.h"
#include "tools.h"
#include "initProgram.h"
#include "login.h"
#include "stuManage.h"
#include "tchManage.h"
#include "headManage.h"


//hehe
bool loginCheck(int identity)
{
	static int loginFailed = 0;
	int account = 0;
	int lastAccount = 0;
	char accountChar[50] = {};
	char password[50] = {};
	//校长登陆
	if(3 == identity)
	{
		int ifSuccess = 0;
		while(0 == ifSuccess)
		{
			system("clear");
			printf("请输入校长帐号\n");
			scanf("%s", accountChar);
			getchar();
			printf("请输入校长密码\n");
			strcpy(password,inputPassword());
			if(0 == strcmp(accountChar,HEADID) && 0 == strcmp(password,HEADPASSWORD))
			{
				if(0 == HEADFLAG)
				{
					char password[50] = {};
					printf("首次登陆需修改密码，请输入密码:\n");
					scanf("%s",password);
					while(ifBeyond(password,20))
					{
						printf("密码过长，请缩减至20位以内！\n");
						printf("请重新输入密码:\n");
						strcpy(password,inputPassword());
						getchar();
					}
					strcpy(HEADPASSWORD,password);
					HEADFLAG = 1;	
					printf("重置成功，请重新登陆！\n");
					anykey_continue();
					login();
				}
				else
				{
					headManage();
				}
			}
			else
			{
				show_msg("帐号或密码有误，请重新输入\n",1.5);
			}
		}
	}
	while((1 ==identity || 2 == identity) && loginFailed < 3)
	{
		system("clear");
		printf("请输入%s：\n",identity==1?"学生学号":"教师工号");
		scanf("%d", &account);
		getchar();
		printf("请输入密码:\n");
		strcpy(password,inputPassword());
		if(account != lastAccount)
		{
			loginFailed = 0;
		}
		lastAccount = account;

		if(1 == identity)
		{
			if(account <2000 && account >=1001)
			{
				Student stu = searchStu(account, password);
				if(stu.flag == -1)
				{
					printf("帐号已锁定，请联系教师解锁\n");
					anykey_continue();
					login();
				}
				if(stu.sex)
				{
					//登陆成功
					switch(stu.flag)
					{
						case 0: //第一次登陆
							firstLogin(identity,stu.id);
							loginFailed = 0;
							login();
							break;
						case 1: //正常登陆
						case 2: 
							loginFailed = 0;
							stuManage(stu.id);
							break;
						case -1: //帐号已锁定
							printf("帐号已锁定，请联系教师解锁\n");
							loginFailed = 0;
							anykey_continue();	
							break;
					}
				}
				else
				{
					loginFailed++;
					if(loginFailed <= 2)
					{
						printf("帐号或密码有误，还有%d机会\n", 3-loginFailed);
						anykey_continue();
						//按任意键继续
					}
					else if(3 == loginFailed)
					{
						lockAccount(identity,account);
						printf("帐号已锁定，请联系教师解锁\n");
						loginFailed = 0;
						anykey_continue();
						login();
					}
				}
			}
			else 
			{
				printf("账号或密码错误！\n");
				anykey_continue();
			}
		}
		else if(2 == identity)
		{
			if(account < 201 && account > 100)
			{
				Teacher tch = searchTch(account, password);
				if(tch.flag == -1)
				{
					printf("帐号已锁定，请联系管理员解锁\n");
					anykey_continue();
					login();
				}
				if(tch.sex) //若返回结构体中sex存在，则表明登陆成功
				{
					loginFailed = 0; //登陆成功，重置尝试次数
					switch(tch.flag)
					{
						case 0: //第一次登陆
							firstLogin(identity,tch.id);
							login();
							break;
						case 1: //正常登陆
							tchManage(tch.id);
							break;
						case 2: //教师离职无法操作
							printf("您已离职，请勿登陆成绩管理系统！\n");
							anykey_continue();
							break;
						case -1: //帐号已锁定
							printf("帐号已锁定，请联系管理员解锁\n");
							anykey_continue();
							break;
					}
				}
				else
				{
					loginFailed++;
					if(loginFailed <= 2)
					{
						printf("帐号或密码有误，还有%d机会\n", 3-loginFailed);
						anykey_continue();//按任意键继续
					}
					else if(3 == loginFailed)
					{
						lockAccount(identity,account);
						printf("帐号已锁定，请联系管理员解锁\n");
						loginFailed = 0;
						anykey_continue();
						login();
					}
				}
			}
			else
			{
				printf("账号或密码错误！\n");
				anykey_continue();
			}
		}	
	}
}

